package mesweb.mesweb.controller;


import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.farvision.common.api.CommonResult;
import com.farvision.common.exception.ApiException;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import mesweb.mesweb.Utils.QueryWrapperUtil;
import mesweb.mesweb.entity.MesBasicAction;
import mesweb.mesweb.service.IMesBasicActionService;
import mesweb.mesweb.vo.MesBasicActionVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author niufw
 * @since 2021-05-19
 */
@RestController
@RequestMapping("/mes/mesbasicaction")
public class MesBasicActionController {

    @Autowired
    private IMesBasicActionService mesBasicActionService;

    @GetMapping("/AllActionTherbligss")
    @ApiOperation("获取所有设备列表")
    public CommonResult<?> AllActionTherbligss()
    {
        List<MesBasicAction> data = new ArrayList<>();
        data =  mesBasicActionService.list();
        return  CommonResult.success(data);
    }

    /**
     * 分页列表查询
     *
     * @param mesBasicAction
     * @param size
     * @param page
     * @param req
     * @return
     */
    @ApiOperation(value="基本动作-分页列表查询", notes="基本动作-分页列表查询")
    @GetMapping(value = "/list")
    public CommonResult<?> queryPageList(MesBasicAction mesBasicAction,
                                         @RequestParam(name="page", defaultValue="1") Integer page,
                                         @RequestParam(name="size", defaultValue="10") Integer size,
                                         HttpServletRequest req) {

        QueryWrapperUtil<MesBasicAction> queryWrapperUtil = new QueryWrapperUtil<>();
        QueryWrapper<MesBasicAction> queryWrapper = queryWrapperUtil.build(MesBasicAction.class,mesBasicAction);
        Page<MesBasicActionVo> p = new Page<MesBasicActionVo>(page, size);
        IPage<MesBasicActionVo> pageList = mesBasicActionService.getList(p, queryWrapper);
        return CommonResult.success(pageList);
    }
    /**
     * 编辑
     *
     * @param mesBasicAction
     * @return
     */
    @ApiOperation(value="基本动作-编辑", notes="基本动作-编辑")
    @PostMapping(value = "/save")
    public CommonResult<?> edit(@RequestBody MesBasicAction mesBasicAction) {

        boolean result = false;
        if(mesBasicAction.getId() == 0L) {
            QueryWrapper<MesBasicAction> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("basic_action_No", mesBasicAction.getBasicActionNo());
            queryWrapper.or().eq("basic_action_Name", mesBasicAction.getBasicActionName());
            MesBasicAction mesTechnologyFlow1 = mesBasicActionService.getOne(queryWrapper);
            if (mesTechnologyFlow1 != null) {
                String msg = StrUtil.format("编号{}或名称{}已经存在！", mesTechnologyFlow1.getBasicActionNo(), mesTechnologyFlow1.getBasicActionName());
                throw new ApiException(msg);
            }
            result = mesBasicActionService.save(mesBasicAction);
        }
        else
        {
            result = mesBasicActionService.updateById(mesBasicAction);
        }
        return CommonResult.success(result);
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @ApiOperation(value="基本动作-通过id删除", notes="基本动作-通过id删除")
    @DeleteMapping(value = "/delete")
    public CommonResult<?> delete(@RequestParam(name="id",required=true) String id) {
        mesBasicActionService.removeById(id);
        return CommonResult.success("删除成功!");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @ApiOperation(value="基本动作-通过id查询", notes="基本动作-通过id查询")
    @GetMapping(value = "/queryById")
    public CommonResult<?> queryById(@RequestParam(name="id",required=true) String id) {
        MesBasicAction mesBasicAction = mesBasicActionService.getById(id);
        return CommonResult.success(mesBasicAction);
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @ApiOperation(value="动作组-批量删除", notes="动作组-批量删除")
    @DeleteMapping(value = "/deleteBatch")
    public CommonResult<?> deleteBatch(@ApiParam("动作组IDs") @RequestBody List<Long> ids) {
        this.mesBasicActionService.removeByIds(ids);
        return CommonResult.success("批量删除成功！");
    }


}
